﻿/**
* @authors Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package {
	
	import c_XMLObject;
	import com.ng.ResHelp.c_Builder;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	
	/**
	 * Wrapper for every object that being loaded into the editor:
	 * it has the object as child, a xml that contains the xml node of the object
	 * and a background in Shape so that we can see it even when the object is set to 
	 * invisible
	 */
	public dynamic class c_ResObject extends c_XMLObject {
		public var changedProperties: Array;
		public var __Class: String;
		public var __Content:DisplayObject;
		public var __Background:Shape;		
		public function Init(item:XML, s:DisplayObject):void {
			xml = item;			
			changedProperties = new Array();
			__Content = s;			
			__Class = xml.attribute('class');			
			__Content.name = xml.attribute("id");
			
			switch(__Class) {
				case  "flash.display.Sprite": 
					trace('Sprite:'+__Content.name);
					__Background = getRectShape(new Rectangle(0, 0, 20, 20));
					break;
				case  "flash.display.SimpleButton": 
					trace('SimpleButton:'+__Content.name);
					__Background = getRectShape(new Rectangle(0, 0, 20, 20));
					break;
				default:			
					__Background = getBackground(__Content);
			}
			
			if (__Content is Sprite) {
				__Content.mouseEnabled = false;
				__Content.mouseChildren = false;
				if (__Content is MovieClip) {
					__Content.stop();
				}
			}
			c_Builder.setAttributes(xml, __Content, getDefinitionByName('flash.display.MovieClip') as Class);
			addChild(__Background);
			addChild(s);
			
			//swap the position (only)
			this.x = __Content.x;
			this.y = __Content.y;
			__Content.x = 0;
			__Content.y = 0;			
		}			
		public function get Content():DisplayObject {
			return __Content;
		}		
		public  function setwidth(value:Number): void {
			__Content.width = value;
			if (changedProperties.indexOf("width") < 0) changedProperties.push("width");
		}
		public function setheigth(value:Number): void {
			__Content.height = value;
			if (changedProperties.indexOf("heigth") < 0) changedProperties.push("heigth");
		}
		public function setscaleX(value:Number): void {
			__Content.scaleX = value;
			if (changedProperties.indexOf("scaleX") < 0) changedProperties.push("scaleX");
		}
		public function setscaleY(value:Number): void {
			__Content.scaleY = value;
			if (changedProperties.indexOf("scaleY") < 0) changedProperties.push("scaleY");
		}
		public function setalpha(value:Number): void {
			__Content.alpha = value;
			if (changedProperties.indexOf("alpha") < 0) changedProperties.push("alpha");
		}
		public function setrotation(value:Number): void {
			__Content.rotation = value;
			if (changedProperties.indexOf("rotation") < 0) changedProperties.push("rotation");
		}
		
		public function setvisible(value:Boolean): void {
			if (value) {
				__Content.visible = true;
			} else {				
				__Content.visible = false;
			}
			if (changedProperties.indexOf("visible") < 0) changedProperties.push("visible");
		}
		private function getBackground(sp:DisplayObject):Shape{
			var rec: Rectangle = sp.getRect(sp);
            return getRectShape(rec);
        }
		private function getRectShape(rec : Rectangle):Shape {
			var child:Shape = new Shape();
			child.alpha = 0.8;
            child.graphics.beginFill(0xB4C4D3);
            child.graphics.drawRect(rec.x, rec.y, rec.width, rec.height);
            child.graphics.endFill();
            return child;
		}
	}

}